<?php
class Tourism_order_product_model extends BZ_Model
{
    protected $table = 'tourism_order_product';
    
    /**
     * 获取订单产品信息
     * @param unknown $orderId
     * @return unknown
     */
    public function getProductByOrderId($orderId)
    {
        $this->db->where('order_id', $orderId);
        return $this->db->get($this->table);
    }
    
    public function add($postData, $order_product_id)
    {
        $data = array(
            'order_id'     => $postData['order_id'],
            'goods_attr_id' => $postData['goods_attr_id'],
            'goods_name'   => $postData['goods_name'],
            'number'       => $postData['number'],
            'price'        => $postData['price'],
            'market_price' => $postData['market_price'],
            'shop_price'   => $postData['shop_price'],
            'supply_price' => $postData['supply_price'],
            'pay_amount'   => $postData['pay_amount'],
            'created_at'   => $postData['created_at']
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function setTourismNum($params)
    {
        if (isset($params['refund_num'])) {
            $this->db->set('refund_num', 'refund_num+'.$params['refund_num'], false);
        }
        if (isset($params['barter_num'])) {
            $this->db->set('barter_num', 'barter_num-'.$params['barter_num'], false);
        }
        $this->db->where('order_id', $params['order_id']);
        $this->db->where('goods_attr_id', $params['goods_attr_id']);
        return $this->db->update($this->table);
    }
    
    /**
     * 获取要更新的商品信息
     * @param unknown $params
     */
    public function findByParamProduct($params)
    {
        if (!empty($params['order_ids'])) {
            $this->db->where_in('order_id', $params['order_ids']);
        }
        if (!empty($params['refund_num'])) {
            $this->db->where('refund_num >', $params['refund_num']);
        }
        return $this->db->get($this->table);
    }
}